package com.example.bwic_svc.controller;

import com.example.bwic_svc.model.Bid;
import com.example.bwic_svc.model.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/bidding")
public class BidController {
    private final CurrentBidService currentBidService = new CurrentBidService();

    @PostMapping("/placeBid")
    public Result placeBid(@RequestBody Bid bid) {
        Double currentBid = currentBidService.getCurrentBid(bid.getItemId());
        if (currentBid == null) {
            currentBidService.updateCurrentBid(bid.getItemId(), bid.getAmount());
            return new Result(true, "出价成功", bid);
        } else if (bid.getAmount() > currentBid) {
            currentBidService.updateCurrentBid(bid.getItemId(), bid.getAmount());
            return new Result(true, "出价成功", bid);
        } else {
            return new Result(false, "出价失败，您的出价未高于当前出价", null);
        }
    }
}